package cn.ljx.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ljx.entity.JsonReturn;
import cn.ljx.entity.TBHandle;
import cn.ljx.service.TBHandleService;
import cn.ljx.util.BaseUtils;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/Handle")
public class HandleController {
	@Autowired
    private TBHandleService tbHandleService;
	
	@SuppressWarnings("finally")
	@ResponseBody
	@RequestMapping(value="/getHandleList",method=RequestMethod.POST)
    public String getHandleList(String real_name){
		JsonReturn jr = new JsonReturn();
		try{
			//Map<>result
			List<TBHandle> tbhandleList=tbHandleService.gettbHandleList(real_name);
	        if(tbhandleList!=null){
	        	jr.setObject(tbhandleList);
	        	jr.setInfo("success");
	        }
		}catch(Exception e){
			jr.setInfo("false");
		}finally{
			return JSON.toJSONString(jr);
		}
    }
	
	@ResponseBody
	@RequestMapping(value="/delectHandle",method=RequestMethod.POST)
	public JsonReturn delectHandle(String login_name){
		JsonReturn jr = new JsonReturn();
		int b = tbHandleService.delectHandle(login_name);
		if(b!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addHandle",method=RequestMethod.POST)
	public JsonReturn addHandle(TBHandle tbhandle){
		JsonReturn jr = new JsonReturn();
		try {
			tbhandle.setId(BaseUtils.getUUID32());
			tbhandle.setCreate_time(BaseUtils.getTime());
			tbhandle.setPassword("123456");
			tbhandle.setState("1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = tbHandleService.addHandle(tbhandle);
		if(i==1){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//获得某一用户的数据
	@ResponseBody
	@RequestMapping(value="/getHandleByName",method=RequestMethod.POST)
	public JsonReturn getHandleByName(String login_name){
		JsonReturn jr = new JsonReturn();
		TBHandle tbHandle = tbHandleService.getHandleByName(login_name);
		if(tbHandle!=null){
			jr.setInfo("success");
			jr.setObject(tbHandle);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//获得当前用户的数据
		@ResponseBody
		@RequestMapping(value="/getHandle",method=RequestMethod.POST)
		public JsonReturn getHandle(String login_name,Model model,HttpSession httpsession){
			JsonReturn jr = new JsonReturn();
			TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
			if(userSession!=null){
				TBHandle tbHandle = tbHandleService.getHandleById(userSession.getId());
				if(tbHandle!=null){
					jr.setInfo("success");
					jr.setObject(tbHandle);
				}else{
					jr.setInfo("false");
				}
			}else{
				jr.setInfo("false");
			}
			return jr;
		}
		
		@ResponseBody
		@RequestMapping(value="/updateHandle",method=RequestMethod.POST)
		public JsonReturn updateHandle(TBHandle tbhandle,Model model,HttpSession httpsession){
			JsonReturn jr = new JsonReturn();
			TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
			tbhandle.setId(userSession.getId());
			int i = tbHandleService.updateHandle(tbhandle);
			if(i!=0){
				jr.setInfo("success");
			}else{
				jr.setInfo("flase");
			}
			return jr;
		}
}
